package com.mz.mattress.api.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import com.mz.mattress.api.enums.ClientEnum;
import lombok.Data;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 微信小程序二维码工具
 */
@Data
@Component
public class WxMinAppUtil {

    @Autowired
    private WxProperties wxProperties;

    @Resource(name = "clientWxMaService")
    private WxMaService clientWxMaService;

    @Resource(name = "merchantWxMaService")
    private WxMaService merchantWxMaService;

    /**
     * 生成小程序二维码的字节（有数量限制）
     * @param path
     * @param width
     * @param autoColor
     * @param lineColor
     * @param isHyaline
     * @return byte
     */
    public byte[] createCodeBytes(ClientEnum clientEnum, String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws Exception {
        return getWxMaService(clientEnum).getQrcodeService().createWxaCodeBytes(path, wxProperties.getEnvVersion(), width, autoColor, lineColor, isHyaline);
    }

    /**
     * 生成小程序二维码的字节（无数量限制）
     * @param scene     参数(name=bb)
     * @param page      页面路径
     * @param checkPath
     * @param width
     * @param autoColor
     * @param lineColor
     * @param isHyaline
     * @return byte
     */
    public byte[] createCodeUnlimitBytes(ClientEnum clientEnum, String scene, String page, boolean checkPath, int width,
                                         boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline) throws Exception {
        return getWxMaService(clientEnum).getQrcodeService().createWxaCodeUnlimitBytes(scene, page, checkPath, wxProperties.getEnvVersion(), width, autoColor, lineColor, isHyaline);
    }

    /**
     * 获取小程序 Short Link
     * @param pageUrl
     * @param pageTitle
     * @param isPermanent
     * @return String
     */
    public String generateShortLink(ClientEnum clientEnum, String pageUrl, String pageTitle, Boolean isPermanent) throws Exception {
        GenerateShortLinkRequest request = GenerateShortLinkRequest.builder()
                .pageUrl(pageUrl)
                .pageTitle(pageTitle)
                .isPermanent(isPermanent)
                .build();
        return getWxMaService(clientEnum).getLinkService().generateShortLink(request);
    }

    /**
     * 获取小程序 URL Link
     * @param path
     * @param query
     * @param isExpire
     * @param expireType
     * @param expireTime
     * @param expireInterval
     * @return String
     */
    public String generateUrlLink(ClientEnum clientEnum, String path, String query, Boolean isExpire, Integer expireType, Long expireTime, Integer expireInterval) throws Exception {
        GenerateUrlLinkRequest request = GenerateUrlLinkRequest.builder()
                .path(path)
                .query(query)
                .envVersion(wxProperties.getEnvVersion())
                .isExpire(isExpire)
                .expireType(expireType)
                .expireTime(expireTime)
                .expireInterval(expireInterval)
                .build();
        return getWxMaService(clientEnum).getLinkService().generateUrlLink(request);
    }

    private WxMaService getWxMaService(@NonNull ClientEnum clientEnum) {
        switch (clientEnum) {
            case MERCHANT:
                return merchantWxMaService;
            case CLIENT:
                return clientWxMaService;
        }
        return null;
    }

}
